package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.domain.ScenicMafengwo;
import cn.wolfcode.luowowo.article.query.ScenicAllQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicMafengwoService;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.article.util.ScenicUtil;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


//景点点评
@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IScenicService scenicService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @Reference
    private IScenicMafengwoService scenicMafengwoService;

    //根据目的地id查询该下面的景点
    @RequestMapping("/list")
    public String list(Model model, Long id, @UserParam UserInfo userInfo){

        model.addAttribute("userInfo",userInfo);

        //根据id查询该目的地
        Destination destination = destinationService.getByDestId(id);
        //根据目的地查询概括
        String gen = destinationService.getGeneralizeByDestId(id);
        model.addAttribute("dest",destination);
        model.addAttribute("gen",gen);

        //查询Top5
        List<Scenic> scenicTop5 = scenicService.getScenicTop5(id);
        model.addAttribute("scenicTop5",scenicTop5);

        //热门景点
        List<ScenicMafengwo> mfwList = scenicMafengwoService.selectAll();
        model.addAttribute("mfwList",mfwList);

        return "scenic/remarkList";
    }

    //评论页面
    @RequestMapping("/remark")
    public String remark(Model model,Long id,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);

        //查询全部
        List<Scenic> list = scenicService.selectAll();
        for (int i=0;i<list.size();i++){
            if(list.get(i).getId() == id){
                list.remove(i);
                break;
            }
        }
        model.addAttribute("listAll",list);

        //共享景点到评论页面
        Scenic scenic = scenicService.getRemarkById(id);
        model.addAttribute("sc",scenic);

        return "scenic/remarkDetail";
    }

    @Value("${file.path}")
    private String path;

    //景点评论
    @RequestMapping("/scenicComment")
    @ResponseBody
    public Object scenicComment(@UserParam UserInfo userInfo, ScenicComment scenicComment, MultipartFile file){

        try {
            if(userInfo == null){
                throw new RuntimeException("先请登录");
            }

            ScenicUtil.hasLength(scenicComment.getRank(),"总价评价不能为空");
            ScenicUtil.hasLength(scenicComment.getSceneRank(),"风光评价不能为空");
            ScenicUtil.hasLength(scenicComment.getFeatureRank(),"特色评价不能为空");
            ScenicUtil.hasLength(scenicComment.getServiceRank(),"服务评价不能为空");
            ScenicUtil.hasLength(scenicComment.getComment(),"评论不能为空");

            if(file != null){
                String upload = UploadUtil.upload(file,path);
                scenicComment.setComImg( "/" + upload);
            }

            scenicComment.setCreatTime(new Date());
            scenicComment.setUserId(userInfo.getId());
            scenicComment.setUserName(userInfo.getNickname());
            scenicComment.setUserLv(Long.valueOf(userInfo.getLevel()));
            scenicComment.setUserImg(userInfo.getHeadImgUrl());

            //把该景点第一张图片放到对象中

            scenicCommentService.save(scenicComment);

            //评论数加1 无奈..
            Scenic scenic = scenicService.getRemarkById(scenicComment.getScenicId());
            scenic.setCommentCount(scenic.getCommentCount() + 1);
            scenicService.updateCount(scenic);

            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    //评论
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") ScenicCommentQuery qo) {

        Page page = scenicCommentService.query(qo);

        model.addAttribute("page", page);

        return "scenic/commentTpl";
    }

    //点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(@UserParam UserInfo userInfo, String scenicCommentId){

        try {

            if(userInfo == null){
                return new RuntimeException("先请登录");
            }

            scenicCommentService.findById(scenicCommentId,userInfo.getId());
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }

    }

    @RequestMapping("/scenicAll")
    public String scenicAll(Model model, @ModelAttribute("qo") ScenicAllQuery qo){

        PageInfo pageInfo = scenicMafengwoService.selectForList(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "scenic/scenicAll";
    }

    //去过数加一
    @RequestMapping("/goAdd")
    @ResponseBody
    public Object goAdd(@UserParam UserInfo userInfo, Long id){ //此id为景点id

        try {
            if (userInfo == null){
                throw new RuntimeException("请先登录");
            }

            Scenic scenic = scenicService.getRemarkById(id);
            String str = scenic.getGoId();
            String[] split = str.split(",");
            for (String s : split) {
                if(userInfo.getId() == Long.valueOf(s)){
                    throw new RuntimeException("您已点过了,请勿重复操作");
                }
            }

            str = str + "," + userInfo.getId();
            scenic.setGoId(str);

            scenicService.goAdd(scenic);
            return AjaxResult.success();

        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }

    }

    //收藏 favorite
    @RequestMapping("/favorite")
    @ResponseBody
    public Object favorite(@UserParam UserInfo userInfo,Long id){ //景点的id

        try {
            if(userInfo == null){
                return new RuntimeException("请先登录");
            }
            Scenic scenic = scenicService.getRemarkById(id);//根据id拿景点
            String favoriteId = scenic.getFavoriteId();
            List<String> list = scenic.getFavoriteIdList();//收藏用户id

            //收藏
            String[] split = favoriteId.split(",");
            for (String s : split) {
                list.add(s);
            }

            if(!list.contains(String.valueOf(userInfo.getId()))){
                favoriteId = favoriteId + "," + userInfo.getId();
                scenic.setFavoriteId(favoriteId);
                scenicService.updateFavorite(scenic);
                return new AjaxResult(true,"收藏成功");
            }

            //----------------------------------------------------


            List<String> newList = new ArrayList<>();
            //取消收藏
            for (String s : split) {
                if(userInfo.getId() == Long.valueOf(s)){ //证明在该数组中
                    continue;
                }else{
                    newList.add(s);
                }
            }

            String str = "";
            for(int i =0;i < newList.size();i++ ){
                if(i < newList.size() -1){
                    str = str + newList.get(i) + ",";
                }else{
                    str = str + newList.get(i);
                }
            }

            scenic.setFavoriteId(str); //重新设置值
            scenicService.updateFavorite(scenic);
            return new AjaxResult(true,"取消收藏成功");

        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }

    }



}
